-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add conversions between typed arrays and Rust #1147
Conversation
For all typed arrays, this commit adds: * `TypedArray::view(src: &[Type])` * `TypedArray::copy_to(&self, dst: &mut [Type])` The `view` function is unsafe because it doesn't provide any guarantees about lifetimes or mutability. The `copy_to` function is, however, safe. Closes rustwasm#811
/// Copy the contents of this JS typed array into the destination | ||
/// Rust slice. | ||
/// | ||
/// This function will efficiently copy the memory from a typed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious, what does "efficiently" mean here? What is the less efficient alternative?
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah this just means that it uses the set
API which eventually translates to something like a raw memcpy
. That's the fastest way (even in Rust) to copy chunks of memory.
The slow way would be to copy bytes one-by-one in wasm which misses on on things like vectorization and eliding bounds checks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gains knowledge
Cool - thanks!!
macro_rules! arrays { | ||
($($name:ident: $ty:ident,)*) => ($( | ||
impl $name { | ||
/// Creates a JS typed array which is a few into wasm's linear |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a minor thing that I noticed -> few
-> view
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I'll be sure to handle before landing!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
This looks great, thanks! Is there a way to return a single element from the typed array as well? There doesn’t seem to be an indexing getter for typed arrays in js-sys, but this is supported in JavaScript. |
@grovesNL Currently you would use let value = js_sys::Reflect::get(typed_array.as_ref(), &JsValue::from_f64(0.0)).unwrap();
It's equivalent to the @alexcrichton @fitzgen Any thoughts on adding a convenience method which basically does the above? I think it'd be especially nice if the convenience method could avoid the If you're okay with it, I can try sending a PR. |
Oh right, using It feels slightly confusing now because of the availability of |
Adding a convenience or a method to just get one item sounds like a good idea to me! @Pauan I'd be down for such a PR |
PR made at #1225 |
For all typed arrays, this commit adds:
TypedArray::view(src: &[Type])
TypedArray::copy_to(&self, dst: &mut [Type])
The
view
function is unsafe because it doesn't provide any guaranteesabout lifetimes or mutability. The
copy_to
function is, however, safe.Closes #811